package com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.PlanDetailBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.StatisticPlanBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CCompanys;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CPdtPlanEx;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CProductsInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CWeightSheets;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.CPdtPlanExMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.CWeightSheetsMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICCompanysService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICPdtPlanExService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICWeightSheetsService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetAllPdtPlanVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetPdtPlanDetailVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Select;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品计划审批表 服务实现类
 * </p>
 *
 * @author xiaoxu
 * @since 2022-12-27
 */
@Service
@DS("szappdb")
public class CPdtPlanExServiceImpl extends ServiceImpl<CPdtPlanExMapper, CPdtPlanEx> implements ICPdtPlanExService {
    @Autowired
    private ICCompanysService companysService;
    @Autowired
    private ICProductsInfoService productsInfoService;
    @Autowired
    private ICWeightSheetsService weightSheetsService;
    @Autowired
    private CWeightSheetsMapper weightSheetsMapper;

    @Override
    public List<GetAllPdtPlanVo> statisticPlan(StatisticPlanBo param) {
        return baseMapper.statisticPlan(param);
    }

    @Override
    public List<GetPdtPlanDetailVo> pdtPlanDetail(PlanDetailBo param) {
        return baseMapper.pdtPlanDetail(param);
    }

    @Override
    public Map<String, Object> queryPdtPlanEx(PlanDetailBo param) {
        IPage<CPdtPlanEx> pager = new Page<>(param.getPage(), param.getPageSize());
        QueryWrapper<CPdtPlanEx> wrapper = new QueryWrapper<>();
        wrapper.eq("PDT_PLANEX_DELETED", 0);

        wrapper.eq(param.getDataFrom() != null, "DATA_FROM", param.getDataFrom());
//        param.getCoId()为int类型，不能为null
        wrapper.eq(param.getCoId() != null && !param.getCoId().equals(""), "PDT_PLANEX_COID", param.getCoId());
//            wrapper.eq(  param.getCoId().equals("") ,"PDT_PLANEX_COID",param.getCoId());
//            wrapper.eq("PDT_PLANEX_PDTID",param.getPdtId());
//            wrapper.eq("PDT_PLANEX_TYPE",param.getPlanType());

//       将param.getCoId()转换为int类型
//        if(StringUtils.isNotBlank(param.getCoId())){
//            wrapper.eq("PDT_PLANEX_COID",param.getCoId());
//        }
        if (StringUtils.isNotBlank(param.getPdtId())) {
            wrapper.eq(param.getPdtId() != null && !param.getPdtId().equals(""), "PDT_PLANEX_PDTID", param.getPdtId());
        }
            wrapper.eq("PDT_PLANEX_DELETED", 0);
            wrapper.eq(param.getDataFrom() != null, "DATA_FROM", param.getDataFrom());
            wrapper.eq(param.getCoId() != null && !param.getCoId().equals(""), "PDT_PLANEX_COID", param.getCoId());
            if (StringUtils.isNotBlank(param.getPdtId())) {
                wrapper.eq(param.getPdtId() != null && !param.getPdtId().equals(""), "PDT_PLANEX_PDTID", param.getPdtId());
            }
            wrapper.eq(param.getPlanType() != null && !param.getPlanType().equals(""), "PDT_PLANEX_TYPE", param.getPlanType());

            if (StringUtils.isNotBlank(param.getMinDate()) && StringUtils.isNotBlank(param.getMaxDate())) {
                wrapper.apply("(" + "(" + "PDT_PLANEX_BEGIN" + " >= " + "'" + param.getMinDate() + "'" + " AND " + " PDT_PLANEX_BEGIN " + " <= " + "'" + param.getMaxDate() + "'" + " ) " + " or " +
                        " ( " + " PDT_PLANEX_END " + " >= " + "'" + param.getMinDate() + "'" + " AND " + " PDT_PLANEX_END " + " <= " + "'" + param.getMaxDate() + "'" + " ) " + " or " +
                        " ( " + "PDT_PLANEX_BEGIN" + " <= " + "'" + param.getMinDate() + "'" + " AND " + " PDT_PLANEX_END " + " >= " + "'" + param.getMaxDate() + "'" + " ) " + " or " +
                        " ( " + " PDT_PLANEX_BEGIN " + " >=" + "'" + param.getMinDate() + "'" + " AND " + " PDT_PLANEX_END " + " <= " + "'" + param.getMaxDate() + "'" + " ) " + ")");
            }
            if (StringUtils.isNotBlank(param.getSortName())) {
                if (StringUtils.isNotBlank(param.getSortString()) && param.getSortString().toUpperCase().equals("\n" +
                        "// DESC")) {
                    wrapper.orderByDesc(param.getSortName());
                } else {
                    wrapper.orderByAsc(param.getSortName());
                }
            } else {
                wrapper.orderByDesc("PDT_PLANEX_ID");
            }
            pager = baseMapper.selectPage(pager, wrapper);
            List<CPdtPlanEx> result = pager.getRecords();
            ModelMapper mapper = new ModelMapper();
            List<GetPdtPlanDetailVo> getPdtPlanDetailVos = mapper.map(result, new TypeToken<List<GetPdtPlanDetailVo>>() {
            }.getType());
            for (GetPdtPlanDetailVo getPdtPlanDetailVo : getPdtPlanDetailVos) {
                QueryWrapper<CCompanys> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("ID", getPdtPlanDetailVo.getPdtPlanexCoid());
                wrapper1.eq("CPY_DELETED", 0);
                CCompanys cCompanys = companysService.getOne(wrapper1);
                getPdtPlanDetailVo.setCoName(cCompanys.getCpyCo() + "." + cCompanys.getCpyChico());
                QueryWrapper<CProductsInfo> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("ID", getPdtPlanDetailVo.getPdtPlanexPdtid());
                wrapper2.ne("PDTINF_DELETED", 1);
                CProductsInfo cProductsInfo = productsInfoService.getOne(wrapper2);
                if (cProductsInfo != null) {
                    getPdtPlanDetailVo.setPdtName(cProductsInfo.getPdtinfPdt());
                    Map map = weightSheetsMapper.getPlanWeight(cCompanys.getCpyCo(), cCompanys.getCpyChico(), cProductsInfo.getPdtinfPdt(), getPdtPlanDetailVo.getPdtPlanexBegin().toString(), getPdtPlanDetailVo.getPdtPlanexEnd().toString(), getPdtPlanDetailVo.getDataFrom());
                    if (map.get("sum") != null && map.get("count") != null) {
                        getPdtPlanDetailVo.setSumNum(BigDecimal.valueOf(Double.parseDouble(map.get("sum").toString())));
                        getPdtPlanDetailVo.setCountNum(BigDecimal.valueOf(Double.parseDouble(map.get("count").toString())));
                    } else {
                        getPdtPlanDetailVo.setSumNum(BigDecimal.valueOf(0));
                        getPdtPlanDetailVo.setCountNum(BigDecimal.valueOf(0));
                    }
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("total", pager.getTotal());
            map.put("list", getPdtPlanDetailVos);
            return map;
        }
}
